package com.fjwt.gz.mgr.ctrl.lite.official;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.OfficialMenuEntity;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.OfficialMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/officialMenus")
public class OfficialMenusController extends CommonCtrl {

    @Autowired
    private OfficialMenuService officialMenusService;

    /**
     * 新增菜单
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_MENU_ADD')")
    @PostMapping("/saveMenus")
    @Transactional(rollbackFor = Exception.class)
    public ApiRes saveMenus(){
        Long userId = getCurrentUser().getSysUserId();
        String appid = getValString("appid");
        String button = getValString("button");
        log.info("pc端新增菜单，请求地址：/api/officialMenus/saveMenus，请求参数：appid：{}，button：{}，sysUserId：{}", appid, button, userId);
        String menuId = officialMenusService.saveMenus(appid, button, userId);
        return !"".equals(menuId) ? ApiRes.ok() : ApiRes.customFail("操作失败！");
    }

    /**
     * 获取菜单
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_MENU')")
    @PostMapping("/getMenus")
    public ApiRes getMenus() {
        String appid = getValString("appid");
        log.info("pc端获取菜单，请求地址：/api/officialMenus/getMenus，请求参数：appid：{}", appid);
        JSONObject json =officialMenusService.getMenus(appid);
        log.info("pc端获取菜单，请求地址：/api/officialMenus/getMenus，返回结果：{}",  JSONObject.toJSONString(json));
        return ApiRes.ok(json);
    }

    /**
     * 删除菜单
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_MENU_DEL')")
    @PostMapping("/removeMenus")
    public ApiRes removeMenus() {
        Long menuId = getValLong("menuId");
        log.info("pc端删除菜单，请求地址：/api/officialMenus/removeMenus，请求参数：menuId：{}", menuId);
        officialMenusService.deleteMenus(menuId);
        return ApiRes.ok();
    }

}
